package com.chen.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.entity.MemberReceiveAddress;
import com.chen.entity.User;
import com.chen.mapper.MemberReceiveAddressMapper;
import com.chen.service.MemberReceiveAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.utils.AddressUtils;
import com.chen.utils.UserThreadLocal;
import com.chen.vo.AddressVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.chen.constant.PortalConstants.DEFAULT_FALSE;
import static com.chen.constant.PortalConstants.DEFAULT_TRUE;

/**
 * <p>
 * 用户收货地址表 服务实现类
 * </p>
 *
 * @author cws
 * @since 2024-11-25
 */
@Service
public class MemberReceiveAddressServiceImpl extends ServiceImpl<MemberReceiveAddressMapper, MemberReceiveAddress> implements MemberReceiveAddressService {

    @Override
    public List<AddressVo> treeList() {
        List<AddressVo> addressVoList = AddressUtils.jsonToAddressList();
        List<AddressVo> firstMenuList = addressVoList.stream().filter
                //过滤出一级菜单
                        (s -> StrUtil.equals(s.getParent(), "2"))
                // 返回list集合形式
                .collect(Collectors.toList());
        // 遍历一级菜单,构造菜单树
        for (AddressVo address : firstMenuList) {
            getChildrenList(address, addressVoList);
        }
        return firstMenuList;
    }

    @Override
    public int create(MemberReceiveAddress address) {
        User user = UserThreadLocal.get();
        address.setUserId(user.getId());
        return this.baseMapper.insert(address);
    }

    @Override
    public int deleteAddress(Long id) {
        User user = UserThreadLocal.get();
        QueryWrapper<MemberReceiveAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id).and(wrapper -> wrapper.eq("user_id", user.getId()));
        return this.baseMapper.delete(queryWrapper);
    }

    @Override
    public int updateAddress(Long id, MemberReceiveAddress address) {
        address.setId(id);
        User user = UserThreadLocal.get();
        Long memberId = user.getId();
        LambdaQueryWrapper<MemberReceiveAddress> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MemberReceiveAddress::getId, id);
        lambdaQueryWrapper.and(wrapper -> wrapper.eq(MemberReceiveAddress::getUserId, memberId));
        return this.baseMapper.update(address, lambdaQueryWrapper);
    }

    @Override
    public int updateDefaultAddr(Long id, Integer status) {
        User user = UserThreadLocal.get();
        MemberReceiveAddress receiveAddress = new MemberReceiveAddress();
        receiveAddress.setId(id);
        receiveAddress.setDefaultStatus(status);
        QueryWrapper<MemberReceiveAddress> queryWrapper = new QueryWrapper<>();
        Long memberId = user.getId();
        queryWrapper.eq("user_id", memberId).eq("default_status", DEFAULT_TRUE);
        MemberReceiveAddress receiveAddress1 = this.baseMapper.selectOne(queryWrapper);
        if (receiveAddress1 != null) {
            if (receiveAddress1.getDefaultStatus() == DEFAULT_TRUE) {
                MemberReceiveAddress record = new MemberReceiveAddress();
                record.setDefaultStatus(0);
                LambdaQueryWrapper<MemberReceiveAddress> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(MemberReceiveAddress::getDefaultStatus, DEFAULT_TRUE)
                        .eq(MemberReceiveAddress::getUserId, memberId);
                this.baseMapper.update(record, lambdaQueryWrapper);
            }
        }
        return this.baseMapper.updateById(receiveAddress);
    }

    @Override
    public List<MemberReceiveAddress> addressList() {
        User user = UserThreadLocal.get();
        return this.baseMapper.selectList(new LambdaQueryWrapper<MemberReceiveAddress>().eq(MemberReceiveAddress::getUserId, user.getId()
        ));
    }

    @Override
    public MemberReceiveAddress getAddressById(Long id) {
        User user = UserThreadLocal.get();
        LambdaQueryWrapper<MemberReceiveAddress> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MemberReceiveAddress::getUserId, user.getId());
        lambdaQueryWrapper.and(wrapper -> wrapper.eq(MemberReceiveAddress::getId, id));
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    /**
     * 递归返回树形省市区三级联动列表
     *
     * @param addressVo
     * @param addressList
     * @return
     */
    private AddressVo getChildrenList(AddressVo addressVo, List<AddressVo> addressList) {
        for (AddressVo address : addressList) {
            if (StrUtil.equals(addressVo.getTreeID(), address.getParent())) {
                addressVo.getChildren().add(getChildrenList(address, addressList));
            }
        }
        return addressVo;
    }

}
